<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
class Interview extends CI_Controller{
    public function __construct() {
        parent::__construct();
        $this->load->model('Cv_model');
        $this->load->model('Interviewer_model');
        $this->load->model('Staff_model');
        $this->load->model('Search_model');
        $this->load->helper('url');
        $this->load->helper('form');
        $this->load->library('form_validation');
        $this->load->library('session');
        $this->load->library('pagination');
    }
    public function loadListCV()
    {
        
        $email=$this->session->userdata('email');
        $data['email']=$email;
        $start_row = $this->uri->segment(3);
        $per_page = 10;
        $config['base_url'] =base_url().'interview/loadListCV';
        $query=$this->Cv_model->getListCV();
        $config['total_rows'] = $query->num_rows();
        $config['per_page'] = $per_page;
        $this->pagination->initialize($config);
        $data['pagination'] = $this->pagination->create_links();
        $data['title_content']="Manage Staff and Salary";
        $data['query']=$this->Cv_model->getCVLimited($per_page,$start_row);
        $this->load->view('_header',$data);
        $this->load->view('CV/loadListCV');
        $this->load->view('_footer');
    }
    public function loadFormViewInterview($CV_ID)
    {
        $email=$this->session->userdata('email');
        $data['email']=$email;
        $data['query']=$this->Interviewer_model->getInformationInterviewByID($CV_ID);
        $data['CV_ID']=$CV_ID;
        $query=$this->Interviewer_model->getFullNameByCVID($CV_ID);
        foreach($query->result() as $row)
        {
            $data['FullName']=$row->FullName;
            $data['CV_Name']=$row->CV_ID."-".$row->FullName;
        }
        $this->load->view('_header',$data);
        $this->load->view('interviewer/loadFormViewInterview');
        $this->load->view('_footer');
        
    }
    public function loadFormInterview($CV_ID)
    {
        $email=$this->session->userdata('email');
        $data['email']=$email;
        $query=$this->Interviewer_model->getFullNameByCVID($CV_ID);
        $data['CV_ID']=$CV_ID;
        $numberinterview=trim($this->security->sanitize_filename($this->input->post('txtNumberInterview')));
        $query=$this->Interviewer_model->getFullNameByCVID($CV_ID);
        foreach($query->result() as $row)
        {
            $sql="select count(Result) as numberinterview from Interview where CV_ID=$CV_ID";
            $query1=$this->db->query($sql);
            foreach($query1->result() as $row1)
            {
            $numberinterview=$row1->numberinterview;
            }
            $query2=$this->Cv_model->getCVByCVID($CV_ID);
            foreach($query2->result() as $row2)
            {
                $PositionInterview=$row2->PositionInterview;
            }
            $data['vitriungtuyen']=explode(",", $PositionInterview);
            $data['FullName']=$row->FullName;
            $data['CV_Name']=$row->CV_ID."-".$row->FullName;
            
                $data['numberofinterview']=$numberinterview+1;
                $this->load->view('_header',$data);
                $this->load->view('interviewer/loadFormInterview');
                $this->load->view('_footer');
            }
            
    }
    public function insertInterview()
    {
        $email=$this->session->userdata('email');
        $data['email']=$email;
        $CV_ID=trim($this->security->sanitize_filename($this->input->post('txtCVID')));
        $TestDay=trim($this->security->sanitize_filename($this->input->post('txtDay')));
        $TestMonth=trim($this->security->sanitize_filename($this->input->post('txtMonth')));
        $TestYear=trim($this->security->sanitize_filename($this->input->post('txtYear')));
        $TestDate=$TestDay."/".$TestMonth."/".$TestYear;
        $Hour=trim($this->security->sanitize_filename($this->input->post('txtHour')));
        $CV_UserName=trim($this->security->sanitize_filename($this->input->post('txtCVUsername')));
        $Round=trim($this->security->sanitize_filename($this->input->post('txtNumberRound')));
        $CV_Status=trim($this->security->sanitize_filename($this->input->post('txtCVStatus')));
        $Interviewer=trim($this->security->sanitize_filename($this->input->post('txtInterviewer')));
        $PositionInterview=trim($this->security->sanitize_filename($this->input->post('txtPositionInterview')));
        if($this->Interviewer_model->insertInterview($TestDate,$Interviewer,$Hour,$CV_ID,$CV_UserName,$CV_Status,$Round,$PositionInterview))
        {
            $this->loadListCV();
        }
        else
            $this->loadFormInterview ($CV_ID);
        
    }
    public function loadFormEditInterview($IC_ID)
    {
        $email=$this->session->userdata('email');
        $data['email']=$email;
        $IC=  explode("-", $IC_ID);
        $Interview_ID=$IC[0];
        $CV_ID=$IC[1];
        $data['Interview_ID']=$Interview_ID;
        $data['CV_ID']=$CV_ID;
        $query=$this->Interviewer_model->getInterviewByInterviewID($Interview_ID);
        foreach ($query->result() as $row)
        {
            $query2=$this->Cv_model->getCVByCVID($CV_ID);
            foreach($query2->result() as $row2)
            {
                $PositionInterview=$row2->PositionInterview;
            }
            $data['vitriungtuyen']=explode(",", $PositionInterview);
            $data['PositionInterview']=$row->PositionInterview;
            $data['CV_UserName']=$row->CV_UserName;
            $data['Result']=$row->Result;
            $UpdateDate=$row->UpdateDate;
            $data['UpdateDay']=  substr($UpdateDate, 0, 2);
            $data['UpdateMonth']=  substr($UpdateDate, 3, 2);
            $data['UpdateYear']=  substr($UpdateDate, 6, 4);
            $data['CV_Status']=$row->CV_Status;
            $this->load->view('_header',$data);
            $this->load->view('interviewer/loadFormEditInterview');
            $this->load->view('_footer');
        }
    }
    public function editInterview()
    {
        $email=$this->session->userdata('email');
        $data['email']=$email;
        $Interview_ID=trim($this->security->sanitize_filename($this->input->post('txtInterview_ID')));
        $CV_ID=trim($this->security->sanitize_filename($this->input->post('txtCV_ID')));
        $UpdateDay=trim($this->security->sanitize_filename($this->input->post('txtDay')));
        $UpdateMonth=trim($this->security->sanitize_filename($this->input->post('txtMonth')));
        $UpdateYear=trim($this->security->sanitize_filename($this->input->post('txtYear')));
        $UpdateDate=$UpdateDay."/".$UpdateMonth."/".$UpdateYear;
        //$Hour=trim($this->security->sanitize_filename($this->input->post('txtHour')));
        $CV_UserName=trim($this->security->sanitize_filename($this->input->post('txtCVUserName')));
        //$Round=trim($this->security->sanitize_filename($this->input->post('txtNumberRound')));
        $CV_Status=trim($this->security->sanitize_filename($this->input->post('txtCVStatus')));
        $Result=trim($this->security->sanitize_filename($this->input->post('txtResult')));
        $PositionInterview=trim($this->security->sanitize_filename($this->input->post('txtPositionInterview')));
        if($this->Interviewer_model->editInterview($Interview_ID,$UpdateDate,$CV_UserName,$CV_Status,$Result,$PositionInterview))
        {
            redirect('interview/loadFormViewInterview/'.$CV_ID);
        }
    }
}
?>
